package com.truckdispatching.push;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.truckdispatching.R;
import com.truckdispatching.activity.SplashActivity;
import com.truckdispatching.http.controller.PushHttpController;
import com.truckdispatching.http.util.OnResponseHandler;

public class PushReciver extends BroadcastReceiver {

	private Context mContext;

	@Override
	public void onReceive(Context context, Intent intent) {
		mContext = context;
		if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
			Intent myIntent = new Intent(context, PushService.class);
			context.startService(myIntent);
		} else if ("com.truckdispatching.push".equals(intent.getAction())) {
			sendPush(mContext);
		} else if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
			sendPush(mContext);
		} else if ("com.truckdispatching.push_stop".equals(intent.getAction())) {
			context.stopService(new Intent(context, PushService.class));
		} else if ("com.truckdispatching.push_start".equals(intent.getAction())) {
			final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
			List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
			boolean isStart = false;
			for (RunningServiceInfo info : services) {
				if ("com.truckdispatching.push.PushService".equals(info.service.getClassName())) {
					isStart = true;
					break;
				}
			}
			if (!isStart) {
				Intent myIntent = new Intent(context, PushService.class);
				context.startService(myIntent);
			}
		}
	}

	private void showMessage(Context aContext, int id, String aContentText) {
		NotificationManager iManager = (NotificationManager) aContext.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification iNote = new Notification(R.drawable.icon, aContentText, System.currentTimeMillis());
		Intent notificationIntent = new Intent(aContext, SplashActivity.class);
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent contentIntent = PendingIntent.getActivity(aContext, 0, notificationIntent,
			PendingIntent.FLAG_UPDATE_CURRENT);
		iNote.setLatestEventInfo(aContext, aContentText, aContentText, contentIntent);
		iNote.defaults = Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND;
		iNote.flags |= Notification.FLAG_AUTO_CANCEL;
		iManager.notify(id, iNote);
	}

	private PushHttpController mPushHttpController;

	private void sendPush(Context aContext) {
		if (mPushHttpController != null) {
			mPushHttpController.cancelHttpRequest();
		} else {
			mPushHttpController = new PushHttpController();
		}
		mPushHttpController.setOnResponseListener(new OnResponseHandler() {
			@Override
			public void onResponseError(int errorCode, String errorMessage) {

			}

			@Override
			public void onResponse(Object response) {
				long timestamp = 0;
				ArrayList<com.truckdispatching.entity.Notification> ns = (ArrayList<com.truckdispatching.entity.Notification>) response;
				com.truckdispatching.entity.Notification notification = null;
				if (ns != null && ns.size() > 0) {
					for (int i = 0; i < ns.size(); i++) {
						if (ns.get(i).time > timestamp) {
							notification = ns.get(i);
							timestamp = notification.time;
						}
					}
					if (notification != null) {
						showMessage(mContext, 1, notification.content);
					}
				}
				SharedPreferences sh = mContext.getSharedPreferences("push_stamp", Context.MODE_PRIVATE);
				sh.edit().putLong("timestamp", timestamp).commit();
			}
		});
		SharedPreferences sh = aContext.getSharedPreferences("push_stamp", Context.MODE_PRIVATE);
		long stamp = sh.getLong("timestamp", 0);
		mPushHttpController.sendRequest(stamp);
	}

}
